package com.bbx.taxi.client.Adapter.City;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean.ChildCitysBean;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Util.ListViewHeight;
import com.bbx.taxi.client.widget.Drag.CityDragLayout;
import com.yanxuwen.MyRecyclerview.MyBaseAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class CityAdapter extends MyBaseAdapter {
	protected LayoutInflater mInflater;
	boolean isStart;
	public View last_expand_sign;
	List<AppCitysBean> list_startcity;
	List<ToCitiesBean> list_endcity;
	private Context context;
	CityDragLayout.OnCityDragViewStatusListener mmOnCityDragViewStatusListener;
	String mselector;
	public CityAdapter(Context mContext,boolean isStart,List<AppCitysBean> list_startcity,List<ToCitiesBean> list_endcity,int headerViewsCount) {
		super(mContext, isStart?list_startcity:list_endcity, headerViewsCount);
		this.isStart=isStart;
		this.context = mContext;
	    this.list_startcity=list_startcity;
		this.list_endcity=list_endcity;
		mInflater = LayoutInflater.from(mContext);
	}
	public List<?> getDatas(){
		if(isStart){
		  return list_startcity;
		}
		return list_endcity;
	}

	public CityAdapter setDatas(List<?> datas) {
		if(isStart){
			list_startcity= (List<AppCitysBean>) datas;
		}else{
			list_endcity= (List<ToCitiesBean>) datas;
		}
		return this;
	}
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		addExpand(R.layout.expand_city);
		return new ViewHolder(setLayout(R.layout.listitem_select_city, parent));
	}

	@Override
	public void onBindViewHolder(BaseViewHolder holder, final int position) {
		final ViewHolder mViewHolder = (ViewHolder) holder;
		if(isStart){
			mViewHolder.tvCity.setText(list_startcity.get(position).getName());
		}else{
			mViewHolder.tvCity.setText(list_endcity.get(position).getName());
		}
		//由于展开项每个item的都不一样，但是item会回收，然后在设置就会导致界面为空，所以我们要在设置一遍视图
			if (getExpandPosition() == holder.getCurPosition()) {
				if(isStart){
					mViewHolder.setChildData(holder, list_startcity.get(position).getChild_citys(), null);

				}else{
					mViewHolder.setChildData(holder, null, list_endcity.get(position).getChild_citys());

				}
				mViewHolder.expand_sign.setVisibility(View.VISIBLE);

			}else{
				mViewHolder.expand_sign.setVisibility(View.GONE);
			}
		super.onBindViewHolder(holder, position);
	}

	@Override
	public int getItemCount() {
		return getDatas() != null ? getDatas().size() : 0;
	}

	public class ViewHolder extends BaseViewHolder {
		@Bind(R.id.tvCity)
		TextView tvCity;
		@Bind(R.id.content)
		View content;
		@Bind(R.id.gview_city)
		GridView gview_city;
		@Bind(R.id.expand_sign)
		public View expand_sign;
		public ViewHolder(View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
		public void setChildData(final MyBaseAdapter.BaseViewHolder holder,
								 final List<ChildCitysBeanX> list_startchildcity, final List<ChildCitysBean> list_endchildcity){
			setChildData(holder, mmOnCityDragViewStatusListener, list_startchildcity, list_endchildcity, mselector);
		}
		/**
		 * 设置子城市
		 * @param list_startchildcity
		 * @param list_endchildcity
		 * @param selector   选中的城市
		 */
		public void setChildData(final MyBaseAdapter.BaseViewHolder holder,
				final CityDragLayout.OnCityDragViewStatusListener mOnCityDragViewStatusListener,
								 final List<ChildCitysBeanX> list_startchildcity, final List<ChildCitysBean> list_endchildcity,String selector){
			mmOnCityDragViewStatusListener=mOnCityDragViewStatusListener;
			mselector=selector;
			try{
				final ChildCityAdapter mChildCityAdapter = new ChildCityAdapter(context, isStart, list_startchildcity, list_endchildcity, selector);
				gview_city.setAdapter(mChildCityAdapter);
				ListViewHeight
						.seGridViewHeightBasedOnChildren(context, gview_city, R.layout.item_childcity);

				gview_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						expand();
						String name="";
						if(isStart){
							 name=list_startchildcity.get(position).getName();
							String detail=list_startchildcity.get(position).getIs_detail();
							mOnCityDragViewStatusListener.onClickCity(name,detail);
						}else{
							 name=list_endchildcity.get(position).getName();
							String detail=list_endchildcity.get(position).getIs_detail();
							mOnCityDragViewStatusListener.onClickCity(name,detail);
						}
						mChildCityAdapter.setSelector(name);
						mChildCityAdapter.notifyDataSetChanged();
					}
				});
			}catch (Exception e){}
		}
		public class ChildCityAdapter extends BaseAdapter {
			private Context mContext;
			List<ChildCitysBeanX> list_startchildcity;
			List<ChildCitysBean> list_endchildcity;
			private boolean isStart;
			private String selector;
			public ChildCityAdapter(Context mContext,boolean isStart,List<ChildCitysBeanX> list_startchildcity,List<ChildCitysBean> list_endchildcity,String selector){
				this.mContext=mContext;
				this.isStart=isStart;
				this.list_startchildcity=list_startchildcity;
				this.list_endchildcity=list_endchildcity;
				this.selector=selector;
			}
			@Override
			public int getCount() {
				return isStart?list_startchildcity.size():list_endchildcity.size();
			}

			@Override
			public Object getItem(int position) {
				return isStart?list_startchildcity.get(position):list_endchildcity.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			public void  setSelector(String selector){
				this.selector=selector;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ChildViewHolder viewholder;
				if(convertView==null){
					LayoutInflater inflater=LayoutInflater.from(mContext);
					convertView=(View)inflater.inflate(R.layout.item_childcity,null);
					viewholder=new ChildViewHolder();
					viewholder.childcity=(TextView)convertView.findViewById(R.id.childcity);
					viewholder.layout=(RelativeLayout)convertView.findViewById(R.id.layout);
					convertView.setTag(viewholder);
				}
				else viewholder=(ChildViewHolder)convertView.getTag();
				String name=isStart?(list_startchildcity.get(position).getName()):(list_endchildcity.get(position).getName());
				viewholder.childcity.setText(name);
				if(name.equals(selector)){
					viewholder.layout.setSelected(true);
					viewholder.childcity.setSelected(true);
				}else{
					viewholder.layout.setSelected(false);
					viewholder.childcity.setSelected(false);
				}

				return convertView;
			}
			public class ChildViewHolder{
				TextView childcity;
				RelativeLayout layout;
			}
		}
	}
}
